package com.aaa.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.aaa.dao.UserinfoDao;
import com.aaa.dao.impl.UserinfoDaoImpl;
import com.aaa.entity.MyReturn;
import com.aaa.entity.Userinfo;
import com.aaa.utils.MyTools;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/5/30 9:36
 * @description：用户信息的控制器
 * @modified By：
 * @version: 1.0
 */
@WebServlet(urlPatterns = "/userinfo")
public class UserinfoServlet extends HttpServlet {
    /**
     *     实例化dao层的对象
     */
    UserinfoDao userinfoDao = new UserinfoDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //将所有的参数存入一个map集合中
        Map<String,Object> map = new HashMap();
        //获取所有的参数名集合
        Enumeration<String> parameterNames = req.getParameterNames();
        //遍历所有的参数名
        while (parameterNames.hasMoreElements()){
           /* System.out.println(parameterNames.nextElement());*/
            //获取第一个参数名
            String key=parameterNames.nextElement();
            //根据参数名获取参数值
            Object value=req.getParameter(key);
            //将参数名和参数值存入map集合中
            map.put(key,value);
        }
        MyReturn myReturn  = new MyReturn();
        switch (map.get("method")+""){
            case "select":
                List<Map<String, Object>> userInfoList = userinfoDao.findAllUserInfoPage(map);
                //将list<map>  转换成list<Userinfo>
                List<Userinfo> userInfoListNew = Convert.convert(new TypeReference<List<Userinfo>>() {
                }, userInfoList);
                for (Userinfo userinfo : userInfoListNew) {
                    System.out.println(userinfo.toString());
                }
                myReturn.setCode(0);
                //获取数据库的总条数
                myReturn.setCount(userinfoDao.getCount(map));
                myReturn.setMsg("");
                myReturn.setData(userInfoListNew);

                break;
            case "save":
                //需要额外处理从页面出入的参数
                String password = map.get("password").toString();
                Map<String, String> passwordSalt = MyTools.makePasswordSalt(password);
                //保存数据到数据库,insert into userinfo ( username, password, salt, deptid, avatar) values (?,?,?,?,?)
                map.put("password",passwordSalt.get("password"));
                map.put("salt",passwordSalt.get("salt"));
                boolean saveUserInfo = userinfoDao.saveUserInfo(map);
                if(saveUserInfo){
                    MyTools.getSuccessResult(myReturn);
                }else
                {
                    MyTools.getFailResult(myReturn);
                }

                break;
            case "delete":
                //删除操作
                boolean deleteUserInfoById = userinfoDao.deleteUserInfoById(map);
                    if(deleteUserInfoById){
                        MyTools.getSuccessResult(myReturn);
                    }else
                    {
                        MyTools.getFailResult(myReturn);
                    }
                break;
            default:
                System.out.println("不支持的方法");
                break;
        }
        //将返回的数据对象进行json字符串转换
        String jsonString = JSON.toJSONString(myReturn);
        //设置响应格式
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(jsonString);



    }
}
